package com.bdqn.sys.controller;



import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.bdqn.common.utitls.DateGindViewResult;
import com.bdqn.common.utitls.JSONResult;
import com.bdqn.common.utitls.SystemConstants;
import com.bdqn.common.utitls.TreeNode;
import com.bdqn.sys.entity.Dept;
import com.bdqn.sys.service.DeptService;
import com.bdqn.sys.vo.DeptVo;
import org.apache.commons.lang3.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.*;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author KazuGin
 * @since 2020-05-01
 */
@RestController
@RequestMapping("/sys/dept")
public class DeptController {


    @Resource
    private DeptService deptService;

    /**
     * 左侧显示部门信息
     * @return
     */
    @RequestMapping("/loadDeptTreeLeft")
    public DateGindViewResult loadDeptTreeLeft(){
        //调用查询所有部门的方法
        List<Dept> deptList = deptService.list();
        //创建树节点的集合
        List<TreeNode> treeNodes=new ArrayList<TreeNode>();
        //循环遍历集合
        for (Dept dept : deptList) {
            //获取展开状态
            Boolean spread=dept.getOpen()== SystemConstants.STATE_OPENT ? true : false;
            treeNodes.add(new TreeNode(dept.getId(),dept.getPid(),dept.getTitle(),spread));
        }
        return new DateGindViewResult(treeNodes);
    }

    /**
     * 点击后右侧显示信息
     */
    @RequestMapping("/list")
    public DateGindViewResult list(DeptVo deptVo){
        //创建分页信息
        IPage<Dept> page=new Page<Dept>(deptVo.getPage(),deptVo.getLimit());
        //创建条件构造器
        QueryWrapper<Dept> queryWrapper=new QueryWrapper<Dept>();
        //部门名称查询
        queryWrapper.like(StringUtils.isNotBlank(deptVo.getTitle()),"title",deptVo.getTitle());
        //部门地址查询
        queryWrapper.like(StringUtils.isNotBlank(deptVo.getAddress()),"address",deptVo.getAddress());
        //部门编号
        queryWrapper.eq(deptVo.getId()!=null,"id",deptVo.getId())
                 .or().eq(deptVo.getId()!=null,"pid",deptVo.getId());
        //调用分页查询的方法
        deptService.page(page,queryWrapper);
        //返回查询结果
        return  new DateGindViewResult(page.getTotal(),page.getRecords());//总页数 总记录数

    }


    /**
     * 部门添加
     */
    @RequestMapping("/addDept")
    public JSONResult AddDept(Dept dept){
        dept.setCreatetime(new Date());//创建时间
        if(deptService.save(dept)){
            return SystemConstants.Add_SUCCESS;
        }
        return SystemConstants.Add_ERROR;
    }

    /**
     * 部门修改
     */
    @RequestMapping("/updateDept")
    public JSONResult updateDept(Dept dept){
        if(deptService.updateById(dept)){
            return SystemConstants.UPDATE_SUCCESS;
        }
        return SystemConstants.UPDATE_ERROR;
    }

    /**
     * 这个方法是用来判断是否存在的子部门的
     * @return
     */
    @RequestMapping("/checkDeptHasChild")
    public String deleteDept(int id){
        Map<String,Object> map=new LinkedHashMap<String,Object>();
        //创建条件构造器
        QueryWrapper<Dept> queryWrapper=new QueryWrapper<Dept>();
        queryWrapper.eq("pid",id);//根据父级编号查询
        if(deptService.count(queryWrapper)>0){
            map.put(SystemConstants.EXIST,true);
        }else {
            map.put(SystemConstants.EXIST,false);
        }
        return JSON.toJSONString(map);
    }


    /**
     * 删除部门
     * @param id
     * @return
     */
    @RequestMapping("/deleteById")
    public JSONResult deleteById(int id){
        if (deptService.removeById(id)){
            return SystemConstants.DELETE_SUCCESS;
        }
        return SystemConstants.DELETE_ERROR;
    }


}

